﻿SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON

GO

ALTER PROCEDURE [dbo].[bhp_NemoBlogPosts_GetLast](
	@Count			INT,
	@ModuleId		INT,
	@OnlyPublic	BIT = 0,
	@Language		NCHAR(5) = 'pl-PL',
	@LastPostId	INT = 0

)
AS
BEGIN

	DECLARE
		@ResTake TABLE(
				Id		INT IDENTITY,
				PostId	INT
			)

	INSERT INTO @ResTake (PostId)
	SELECT 
		idx 
	FROM 
		BLOG_POSTS
	WHERE
		(@OnlyPublic = 0 OR published = 1)
		AND (@ModuleId = 0 OR moduleId = @ModuleId)
		AND del = 0
	ORDER BY ISNULL(publishDate,  dbo.n_DateTimeToDT(ctime)) DESC

	IF @LastPostId > 0 
		SELECT @LastPostId = Id FROM @ResTake WHERE PostId = @LastPostId

	SELECT TOP(@Count)
		p.* 
	FROM 
		@ResTake x
		INNER JOIN BLOG_POSTS p ON x.PostId = p.idx
	WHERE
		@LastPostId = 0 OR x.Id > @LastPostId
	ORDER BY x.Id
		

END

GO

